package gui.product;

import java.util.Calendar;
import java.util.GregorianCalendar;

import core.Amount;
import core.Barcode;
import core.CoreFacade;
import core.Product;
import core.Unit;
import gui.common.*;

/**
 * Controller class for the edit product view.
 */
public class EditProductController extends Controller 
										implements IEditProductController {
	
	
	private CoreFacade core;
	private String productBarcode;
	private boolean canAdd, validAmount, validInts;
	private boolean validDescription, countBox;
	private String description;
	private String shelfLife;
	private String sizeValue;
	private String threeMonthSupply;
	private SizeUnits units;
	private Product oldProduct;
	
	/**
	 * Constructor.
	 * 
	 * @param view Reference to the edit product view
	 * @param target Product being edited
	 */
	public EditProductController(IView view, ProductData target) {
		super(view);
		
		canAdd = validAmount = validInts = validDescription = true;
		
		core = CoreFacade.getInstance();
		
		oldProduct = (Product)target.getTag();
		
		productBarcode = oldProduct.getBarcodeString();
		
		description = oldProduct.getDescription();
		
		shelfLife = Integer.toString(oldProduct.getShelfLife());
		
		sizeValue = Float.toString(oldProduct.getAmount().getSize());
		
		threeMonthSupply = Integer.toString(oldProduct.getThreeMonthSupply());
		
		units = oldProduct.getAmount().getUnit().toSizeUnits();
		
		countBox = (units.toUnit() != Unit.count);
		
		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IEditProductView getView() {
		return (IEditProductView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() 
	{
		getView().enableOK(canAdd);
		getView().enableSizeValue(countBox);
		getView().enableBarcode(false);
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() 
	{
		getView().setBarcode(productBarcode);
		getView().setDescription(description);
		getView().setShelfLife(shelfLife);
		getView().setSizeUnit(units);
		getView().setSizeValue(sizeValue);
		getView().setSupply(threeMonthSupply);
	}

	//
	// IEditProductController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * edit product view is changed by the user.
	 */
	@Override
	public void valuesChanged() 
	{
		description = getView().getDescription(); // string
		shelfLife = getView().getShelfLife(); // string
		units = getView().getSizeUnit(); // sizeUnits
		sizeValue = getView().getSizeValue(); // string
		threeMonthSupply = getView().getSupply(); // string
		
		if(units == SizeUnits.Count)
		{
			sizeValue = "1";
			getView().setSizeValue(sizeValue);
		}
		
		float f;
		try{
			f = Float.parseFloat(sizeValue);
		} catch (Exception e){
			f = -1;
		}
		Amount amt = new Amount(units.toUnit(),f);
		
		int shelf_life;
		try{
			shelf_life = Integer.parseInt(shelfLife);
		}catch (Exception e)
		{
			shelf_life = -1;
		}
		int three_month_supply;
		try{
			three_month_supply = Integer.parseInt(threeMonthSupply);
		}catch (Exception e)
		{
			three_month_supply = -1;
		}

		//count box will be disables IFF unit type is count
		countBox = (amt.getUnit() != Unit.count);
		//check amount is valid
		validAmount = amt.isValidAmount();
		//check the others are non negative
		validInts = (shelf_life > -1) && (three_month_supply > -1);
		// check description is non empty
		validDescription = !description.isEmpty();
		// all of the above must be true for the ok button to be up.
		canAdd = validAmount && validInts && validDescription;
		enableComponents();
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the edit product view.
	 */
	@Override
	public void editProduct() 
	{
		Calendar newDate = new GregorianCalendar();
		Barcode bCode = new Barcode(productBarcode);
		Amount amt = new Amount(units.toUnit(),Float.parseFloat(sizeValue));
		int shelf_life = Integer.parseInt(shelfLife);
		int three_month_supply = Integer.parseInt(threeMonthSupply);

		Product newProductinfo = new Product(newDate, bCode, description, amt,
				 shelf_life, three_month_supply);
		try {
			core.editProduct(oldProduct, newProductinfo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

